/*
 * @Author: your name
 * @Date: 2020-10-25 15:01:11
 * @LastEditTime: 2021-03-03 23:44:58
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \esp-adf\examples\ASR\main\periph\buttonTask.c
 */
#include "buttonTask.h"
#include "irTask.h"
#include "dht11.h"
#include "myhttp.h"
#include "player.h"
#include "myds18b20.h"
#include "clock.h"
#include "driver/gpio.h"

static const char *TAG = "buttonTask";


TaskHandle_t buttonTask_handle;
#if 0
static void IRAM_ATTR gpio_isr_handler(void *agr)
{
    uint32_t gpio_num = (uint32_t) agr;
    switch (gpio_num)
    {
    case KEY1:
        printf("key1\r\n");
        break;
    case KEY2:
        printf("key2\r\n");
        break;
    default:
        break;
    }
}
/*
 * 板子所有io的初始化
 */
int all_gpio_init()
{
    esp_err_t err = 0;
    gpio_config_t key1, key2, led;

    key1.pin_bit_mask = (1ULL << KEY1);
    key1.pull_down_en = 0;
    key1.pull_up_en = 1;
    key1.mode = GPIO_MODE_INPUT;
    key1.intr_type = GPIO_PIN_INTR_NEGEDGE; //下降沿触发
    err = gpio_config(&key1);
    gpio_set_intr_type(KEY1, GPIO_PIN_INTR_NEGEDGE);
    if (err != ESP_OK)
    {
        ESP_LOGI(TAG, "KEY1 FAIL");
        return ESP_FAIL;
    }
    key2.pin_bit_mask = (1ULL << KEY2);
    key2.pull_down_en = 0;
    key2.pull_up_en = 1;
    key2.mode = GPIO_MODE_INPUT;
    key2.intr_type = GPIO_PIN_INTR_NEGEDGE; //下降沿触发
    err = gpio_config(&key2);
    gpio_set_intr_type(KEY2, GPIO_PIN_INTR_NEGEDGE);
    if (err != ESP_OK)
    {
        ESP_LOGI(TAG, "KEY2 FAIL");
        return ESP_FAIL;
    }
    gpio_install_isr_service(0);
    gpio_isr_handler_add(KEY1, gpio_isr_handler, (void *)KEY1);
    gpio_isr_handler_add(KEY2, gpio_isr_handler, (void *)KEY2);

    led.mode = GPIO_MODE_OUTPUT;
    led.pin_bit_mask = (1ULL << LED);
    led.intr_type = GPIO_INTR_DISABLE;
    led.pull_down_en = 0;
    led.pull_up_en = 0;
    err = gpio_config(&led);
    if (err != ESP_OK)
    {
        
        ESP_LOGI(TAG, "led fail");
        return ESP_FAIL;
    }
    gpio_set_level(GPIO_NUM_18, 1);
    return ESP_OK;
}

/*
 * 按键处理任务
 */
void buttonTask(void *agr)
{

    esp_log_level_set(TAG, ESP_LOG_INFO);
    
    while (1)
    {
        if(!gpio_get_level(KEY1))
        {
            vTaskDelay(60 / portTICK_RATE_MS);
            if(!gpio_get_level(KEY1))
            {
                
                printf("key1\r\n");
                ir_rx("test");
            }
                
        }
        if(!gpio_get_level(KEY2))
        {
            vTaskDelay(60 / portTICK_RATE_MS);
            if(!gpio_get_level(KEY2))
            {
                
                printf("key2\r\n");
                ir_tx_nvs("test");
            }
                
        }

        vTaskDelay(10 / portTICK_RATE_MS);
    }
}
#endif
#if 1
/*
* 按键任务
*/
void buttonTask(void *agr)
{

    esp_log_level_set(TAG, ESP_LOG_INFO);
    esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG();
    esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg); //初始化空的外设集合

    //初始化按键外设
    ESP_LOGI(TAG, "[1.1] Initialize keys on board");
    audio_board_key_init(set);

    ESP_LOGI(TAG, "[ 1.2 ] Set up  event listener");
    audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
    audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg); //初始化音频事件接口

    //监听按键的事件
    ESP_LOGI(TAG, "[1.3] Listening event from peripherals");

    audio_event_iface_set_listener(esp_periph_set_get_event_iface(set), evt);                             //使用evt监听periph_evt_if

    while (1)
    {
        audio_event_iface_msg_t msg;                                          //事件消息的接口
        esp_err_t ret = audio_event_iface_listen(evt, &msg, portMAX_DELAY); //监听外设事件接口 并将消息放入msg
        if (ret != ESP_OK)
        {
            ESP_LOGI(TAG, "[ * ] Event interface error : %d", ret);
            continue;
        }
        //外设消息是按键按下
        if ((msg.source_type == PERIPH_ID_TOUCH || msg.source_type == PERIPH_ID_BUTTON || msg.source_type == PERIPH_ID_ADC_BTN) && (msg.cmd == PERIPH_TOUCH_TAP || msg.cmd == PERIPH_BUTTON_PRESSED || msg.cmd == PERIPH_ADC_BUTTON_PRESSED))
        {
            ESP_LOGI(TAG, " KEY press");
            //消息内容
            if ((int)msg.data == KEY1)
            {
                ESP_LOGI(TAG, " KEY1 press");
            }
            else if ((int)msg.data == KEY2)
            {
                ESP_LOGI(TAG, " KEY2 press");
               
            }
        }
    }
}
#endif
